package top.idetect.client.modules.cloth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import top.idetect.client.modules.cloth.model.Clothinfo;
import top.idetect.client.modules.cloth.mapper.ClothinfoMapper;
import top.idetect.client.modules.cloth.service.ClothinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wjh
 * @since 2024-03-15
 */
@Service
public class ClothinfoServiceImpl extends ServiceImpl<ClothinfoMapper, Clothinfo> implements ClothinfoService {

    @Autowired
    private ClothinfoMapper clothinfoMapper;

    @Override
    public Page<Clothinfo> page(Integer pageSize, Integer pageNum, String keyword, String deviceName) {
        Page<Clothinfo> page = new Page(pageNum, pageSize);
        QueryWrapper<Clothinfo> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Clothinfo> lambda = wrapper.lambda();
        if(StrUtil.isNotEmpty(keyword)) {
            lambda.and(e-> e.like(Clothinfo::getClothSerial,keyword)
                    .or().like(Clothinfo::getOperator,keyword)
                    .or().like(Clothinfo::getBarCode,keyword)
                    .or().like(Clothinfo::getDeviceName,keyword)
                    .or().like(Clothinfo::getGroupteam,keyword));
        }
        if(StrUtil.isNotEmpty(deviceName)) {
            lambda.like(Clothinfo::getDeviceName, deviceName);
        }
        return page(page, wrapper);
    }

    @Override
    public Clothinfo getByClothSerial(String clothSerial) {
        return getOne(new QueryWrapper<Clothinfo>().lambda().eq(Clothinfo::getClothSerial, clothSerial));
    }

    @Override
    public void updateTotalCount(String clothSerial) {
        clothinfoMapper.updateTotalCount(clothSerial);
    }
}
